//
// Created by YE on 2024/1/5.
//
#include "reader.h"


Reader::Reader()=default;

Reader_State Reader::read_file(const char *file_path) {
    std::fstream file(file_path);
    if (file.is_open()) {
        std::stringstream buffer;
        buffer << file.rdbuf();
        str = buffer.str() + '\0', ch = str[0], line = 1,ready = true;
    } else {
        throw_error(YCH_READER_ERROR, std::string(file_path) + " file not found");
        ready = false;
    }
    file.close();
    return ready;
}

char Reader::get_pre_char() {
    return str[pos - 1];
}

char Reader::next_char() {
    if (ch == '\n'){
        line_start_pos= pos + 1;
        line++;
    }
    if (ch != '\0') {
        ch = str[++pos];
        return ch;
    }
    return ch;
}

Reader::~Reader() = default;